package com.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.NewsAdvertisement;
import com.base.service.LogsService;
import com.base.service.NewsAdvertisementService;
import com.base.utils.Page;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_xinwenguanggao {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private NewsAdvertisementService newsAdvertisementService;

	@RequestMapping("/content_xinwenguanggao")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		// 登录的用户ID 以及操作权限
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Page page = new Page();
		page.setCurrentPageNo(1);
		page.setRecordTotal(newsAdvertisementService.countByParentid(adminUserId));
		List<NewsAdvertisement> newsAdvertisement = newsAdvertisementService.selectByParentid(page, adminUserId);
	
		request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
		if(newsAdvertisement != null && newsAdvertisement.size() > 0) {
			request.setAttribute("position", newsAdvertisement.get(0).getPosition());
		}
		request.setAttribute("newsAdvertisement", newsAdvertisement);
		modelAndView.setViewName("jsp/content_xinwenguanggao");
		return modelAndView;
	}
	
	@RequestMapping("/selectNewsAdvertisement")
	public ModelAndView selectNewsAdvertisement(HttpServletRequest request, Integer curPage) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
	
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
		page.setCurrentPageNo(curPage);
		page.setRecordTotal(newsAdvertisementService.countByParentid(adminUserId));
		List<NewsAdvertisement> newsAdvertisement = newsAdvertisementService.selectByParentid(page, adminUserId);
		
		
		
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		if(newsAdvertisement != null && newsAdvertisement.size() > 0) {
			request.setAttribute("position", newsAdvertisement.get(0).getPosition());
		}

		// 设置页面进行跳转
		modelAndView.setViewName("jsp/content_xinwenguanggao");
		request.setAttribute("newsAdvertisement", newsAdvertisement);
		
		return modelAndView;
	}
}
	
